package view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import model.complaintModel;
import model.complaint_TypeModel;
import model.userModel;

import com.toedter.calendar.JDateChooser;
import common.loadData;

import dao.comboId;
import dao.complaint_typeDao;
import dao.complaintdao;
import dao.userdao;

public class addComplaint extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel lbicon = null;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JPanel PanelComplaintant = null;
	private JLabel lbname = null;
	private JLabel lbAge = null;
	private JLabel lbsex = null;
	private JTextField txtname = null;
	private JTextField txtage = null;
	private JRadioButton radioMale = null;
	private JRadioButton Radiofemale = null;
	private JLabel lbPhone = null;
	private JLabel lbEmail = null;
	private JLabel lbAddress = null;
	private JTextField txtPhone = null;
	private JTextField txtEmail = null;
	private JTextField txtAddress = null;
	private JPanel Panelevent = null;
	private JLabel lbNameComplainted = null;
	private JTextField txtComplaint_name = null;
	private JLabel lbtype = null;
	private JComboBox ComboBoxComplaintype = null;
	private JLabel lbevent = null;
	private JLabel lbDate = null;
	private JDateChooser txtDate;
	private JTextArea txtEvent = null;
	private JButton btnOK = null;
	private JButton btnCancel = null;
	private JLabel lbPlace = null;
	private JTextField txtPlace = null;
	private JLabel lbUser = null;
	private JTextField txtUser = null;
	private ButtonGroup goupsex=null;
	private String table="complaint_type";

	public addComplaint() {
		super();
		initialize();
		loadDataByID();
	}

	private void initialize() {
		this.setSize(630, 592);
		this.setResizable(false);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/List.png")));
		this.setName("addComplaint");
		this.setContentPane(getJContentPane());
		this.setTitle("Add Complaint");
		this.setVisible(true);
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbicon = new JLabel();
			lbicon.setBounds(new Rectangle(63, 8, 49, 49));
			lbicon.setText("");
			lbicon.setIcon(new ImageIcon(getClass().getResource("/image/paper_content_chart_48.png")));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lbicon, null);
			jContentPane.add(getPanelTitle(), null);
			jContentPane.add(getPanelComplaintant(), null);
			jContentPane.add(getPanelevent(), null);
			jContentPane.add(getBtnOK(), null);
			jContentPane.add(getBtnCancel(), null);
		}
		return jContentPane;
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setText("Add Complaint Register");
			lbtitle.setSize(new Dimension(209, 30));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 18));
			lbtitle.setForeground(new Color(31, 122, 122));
			lbtitle.setLocation(new Point(0, 0));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.setSize(new Dimension(208, 28));
			panelTitle.setLocation(new Point(121, 20));
			panelTitle.add(lbtitle, null);
		}
		return panelTitle;
	}

	private JPanel getPanelComplaintant() {
		if (PanelComplaintant == null) {
			lbAddress = new JLabel();
			lbAddress.setText("Address:");
			lbAddress.setLocation(new Point(310, 106));
			lbAddress.setSize(new Dimension(61, 24));
			lbEmail = new JLabel();
			lbEmail.setText("Email:");
			lbEmail.setSize(new Dimension(60, 26));
			lbEmail.setLocation(new Point(310, 61));
			lbPhone = new JLabel();
			lbPhone.setText("Phone:");
			lbPhone.setSize(new Dimension(60, 26));
			lbPhone.setLocation(new Point(310, 20));
			lbsex = new JLabel();
			lbsex.setBounds(new Rectangle(13, 104, 62, 24));
			lbsex.setText("Sex:");
			lbAge = new JLabel();
			lbAge.setText("Age:");
			lbAge.setSize(new Dimension(60, 26));
			lbAge.setLocation(new Point(14, 63));
			lbname = new JLabel();
			lbname.setText("Name:");
			lbname.setLocation(new Point(15, 20));
			lbname.setSize(new Dimension(60, 26));
			PanelComplaintant = new JPanel();
			PanelComplaintant.setLayout(null);
			PanelComplaintant.setBounds(new Rectangle(16, 67, 595, 156));
			PanelComplaintant.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(153, 153, 255), 2), "Person Complaintant", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(102, 102, 255)));
			PanelComplaintant.add(lbname, null);
			PanelComplaintant.add(lbAge, null);
			PanelComplaintant.add(lbsex, null);
			PanelComplaintant.add(getTxtname(), null);
			PanelComplaintant.add(getTxtage(), null);
			PanelComplaintant.add(getRadioMale(), null);
			PanelComplaintant.add(getRadiofemale(), null);
			goupsex = new ButtonGroup();
			goupsex.add(getRadioMale());
			goupsex.add(getRadiofemale());
			PanelComplaintant.add(lbPhone, null);
			PanelComplaintant.add(lbEmail, null);
			PanelComplaintant.add(lbAddress, null);
			PanelComplaintant.add(getTxtPhone(), null);
			PanelComplaintant.add(getTxtEmail(), null);
			PanelComplaintant.add(getTxtAddress(), null);
		}
		return PanelComplaintant;
	}

	private JTextField getTxtname() {
		if (txtname == null) {
			txtname = new JTextField();
			txtname.setSize(new Dimension(167, 25));
			txtname.setLocation(new Point(89, 20));
		}
		return txtname;
	}

	private JTextField getTxtage() {
		if (txtage == null) {
			txtage = new JTextField();
			txtage.setLocation(new Point(89, 61));
			txtage.setEditable(true);
			txtage.setSize(new Dimension(167, 25));
			//txtage.setText("o");
		}
		return txtage;
	}

	private JRadioButton getRadioMale() {
		if (radioMale == null) {
			radioMale = new JRadioButton();
			radioMale.setBounds(new Rectangle(90, 106, 75, 24));
			radioMale.setText("Male");
		}
		return radioMale;
	}

	private JRadioButton getRadiofemale() {
		if (Radiofemale == null) {
			Radiofemale = new JRadioButton();
			Radiofemale.setText("Female");
			Radiofemale.setLocation(new Point(165, 107));
			Radiofemale.setSize(new Dimension(89, 22));
		}
		return Radiofemale;
	}

	private JTextField getTxtPhone() {
		if (txtPhone == null) {
			txtPhone = new JTextField();
			txtPhone.setLocation(new Point(401, 20));
			txtPhone.setSize(new Dimension(167, 25));
		}
		return txtPhone;
	}

	private JTextField getTxtEmail() {
		if (txtEmail == null) {
			txtEmail = new JTextField();
			txtEmail.setLocation(new Point(401, 60));
			txtEmail.setSize(new Dimension(167, 25));
		}
		return txtEmail;
	}

	private JTextField getTxtAddress() {
		if (txtAddress == null) {
			txtAddress = new JTextField();
			txtAddress.setLocation(new Point(401, 104));
			txtAddress.setSize(new Dimension(167, 25));
		}
		return txtAddress;
	}

	private JPanel getPanelevent() {
		if (Panelevent == null) {
			lbUser = new JLabel();
			lbUser.setBounds(new Rectangle(339, 59, 96, 24));
			lbUser.setText("User:");
			lbPlace = new JLabel();
			lbPlace.setBounds(new Rectangle(16, 59, 118, 26));
			lbPlace.setText("Place:");
			lbDate = new JLabel();
			lbDate.setText("Date:");
			lbDate.setLocation(new Point(336, 96));
			lbDate.setSize(new Dimension(86, 24));
			lbevent = new JLabel();
			lbevent.setText("Event:");
			lbevent.setLocation(new Point(14, 96));
			lbevent.setSize(new Dimension(66, 24));
			lbtype = new JLabel();
			lbtype.setText("Complaint Type:");
			lbtype.setLocation(new Point(340, 17));
			lbtype.setSize(new Dimension(91, 24));
			lbNameComplainted = new JLabel();
			lbNameComplainted.setText("Complainted Name:");
			lbNameComplainted.setSize(new Dimension(120, 26));
			lbNameComplainted.setLocation(new Point(15, 17));
			Panelevent = new JPanel();
			Panelevent.setLayout(null);
			Panelevent.setBounds(new Rectangle(19, 249, 592, 268));
			Panelevent.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(102, 102, 255), 2), "Detail", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(102, 102, 255)));
			Panelevent.add(lbNameComplainted, null);
			Panelevent.add(getTxtComplaint_name(), null);
			Panelevent.add(lbtype, null);
			Panelevent.add(getComboBoxComplaintype(), null);
			Panelevent.add(lbevent, null);
			Panelevent.add(lbDate, null);
			Panelevent.add(getTxtDate(), null);
			Panelevent.add(getTxtEvent(), null);
			Panelevent.add(lbPlace, null);
			Panelevent.add(getTxtPlace(), null);
			Panelevent.add(lbUser, null);
			Panelevent.add(getTxtUser(), null);
		}
		return Panelevent;
	}

	private JTextField getTxtComplaint_name() {
		if (txtComplaint_name == null) {
			txtComplaint_name = new JTextField();
			txtComplaint_name.setSize(new Dimension(167, 25));
			txtComplaint_name.setLocation(new Point(150, 17));
		}
		return txtComplaint_name;
	}

	private JComboBox getComboBoxComplaintype() {
		if (ComboBoxComplaintype == null) {
			ComboBoxComplaintype = new JComboBox();
			ComboBoxComplaintype.setSize(new Dimension(130, 25));
			ComboBoxComplaintype.setLocation(new Point(446, 17));
			ArrayList<complaint_TypeModel> type = complaint_typeDao.getAllComplaint_type();
			for(complaint_TypeModel model : type)
			{
				ComboBoxComplaintype.addItem(model.getName());
			}
		}
		return ComboBoxComplaintype;
	}
	
	private JDateChooser getTxtDate() {
		if (txtDate == null) {
			txtDate = new JDateChooser();
			txtDate.setDateFormatString("MM/dd/yyyy");
			txtDate.setSize(new Dimension(130, 25));
			txtDate.setLocation(new Point(445, 96));
			txtDate.getDateEditor().setEnabled(false);
		}
		return txtDate;
	}

	private JTextArea getTxtEvent() {
		if (txtEvent == null) {
			txtEvent = new JTextArea();
			txtEvent.setRows(5);
			txtEvent.setLocation(new Point(15, 130));
			txtEvent.setSize(new Dimension(560, 118));
		}
		return txtEvent;
	}

	private JButton getBtnOK() {
		if (btnOK == null) {
			btnOK = new JButton();
			btnOK.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnOK.setIcon(new ImageIcon(getClass().getResource("/image/OK.png")));
			btnOK.setText("Add");
			btnOK.setLocation(new Point(503, 524));
			btnOK.setSize(new Dimension(91, 28));
			btnOK.setHorizontalAlignment(SwingConstants.LEFT);
			btnOK.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					//validate name
					 Pattern username = Pattern.compile("^[a-z]{3,15}$");
				     Matcher mu = username.matcher(txtname.getText());
				     boolean rsName = mu.find();
				     //age
				     Pattern age = Pattern.compile("^[0-9]{1,2}$");
				     Matcher mage = age.matcher(txtage.getText());
				     boolean rsAge = mage.find();
				     //phone
				     Pattern phone = Pattern.compile("^[0-9]{10,11}$");
				     Matcher mphone = phone.matcher(txtPhone.getText());
				     boolean rsPhone = mphone.find();
				     //email ^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
				     Pattern mail = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
				     Matcher mmail = mail.matcher(txtEmail.getText());
				     boolean rsmail = mmail.find();
				     //address
				     Pattern address = Pattern.compile("^[a-zA-Z0-9 ]{5,100}$");
				     Matcher madress = address.matcher(txtAddress.getText());
				     boolean resultadd = madress.find();
				     //complaint name
				     Pattern comName = Pattern.compile("^[a-zA-Z0-9 ]{5,30}$");
				     Matcher mcomName = comName.matcher(txtComplaint_name.getText());
				     boolean resultcomName = mcomName.find();
				     //place
				     Pattern place = Pattern.compile("^[a-zA-Z0-9 ]{5,30}$");
				     Matcher mplace = place.matcher(txtPlace.getText());
				     boolean resultplace = mplace.find();
				     //event
				     Pattern event = Pattern.compile("^[a-zA-Z0-9-_., ]{5,1000}$");
				     Matcher mevent = event.matcher(txtEvent.getText());
				     boolean resultevent = mevent.find();
				     
				     if(txtname.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter name!");
				     }
				     else if(rsName==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Name wrong!");
				     }
				     else if(txtage.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter age!");
				     }
				     else if(rsAge==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "not string!");
				     }
				     else if((Integer.parseInt(txtage.getText())) >= 10)
				     {
				    	 JOptionPane.showMessageDialog(null, "Age greater than 9 and less 100!");
				     }
				     else if(!radioMale.isSelected()&&!Radiofemale.isSelected())
				     {
				    	 JOptionPane.showMessageDialog(null, "Choice sex for user!");
				     }
				     else if(txtPhone.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter phone number!");
				     }
				     else if(rsPhone==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "phone number wrong!"); 
				     }
				     else if(txtEmail.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter Email!");
				     }
				     else if(rsmail==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Email wrong!");
				     }
				     else if(txtAddress.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Emter adress!");
				     }
				     else if(resultadd==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Address wrong!");
				     }
				     else if(txtComplaint_name.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter complaint name!"); 
				     }
				     else if(resultcomName==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Complaint name wrong!");
				     }
				     else if(txtPlace.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter palce!");
				     }
				     else if(resultplace==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Place wrong!");
				     }
				     else if(txtEvent.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter event!");
				     }
				     else if(resultevent==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Event greater than 5 and least 1000!");
				     }
				     else
				     {
				    	// new complaint
							complaintModel model = new complaintModel();
							if(panelComplaint.IdCom>=0)
							{
								model.setId(panelComplaint.IdCom);
							}
							int id_type = comboId.getIdCombo(table, ComboBoxComplaintype.getSelectedItem().toString());
							model.setID_com_type(id_type);
							model.setName(txtname.getName());
							model.setAge(Integer.parseInt(txtage.getText()));
							if(radioMale.isSelected())
							{
								model.setSex(1);
							}
							else
							{
								model.setSex(0);
							}
							model.setAddress(txtAddress.getText());
							model.setPhone(txtPhone.getText());
							model.setEmail(txtEmail.getText());
							model.setDetail(txtEvent.getText());
								SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
								String da = sdf.format(txtDate.getDate());
							model.setDate(da);
							model.setPlace(txtPlace.getText());
							model.setIduser(mainForm.id);
							model.setComplaint_name(txtComplaint_name.getText());
							java.util.Date now = new java.util.Date();
							String d = sdf.format(now);
							model.setLast_updated(d);
							//update complaint
							if(panelComplaint.IdCom>=0)
							{
								int rs = complaintdao.updateComplaint(model);
								if(rs==1)
								{
									JOptionPane.showMessageDialog(null, "Update sucessful!");
									if(login.idrole==0)
										loadData.tab=5;
									else
										loadData.tab=4;
								}
								else
								{
									JOptionPane.showMessageDialog(null, "Update failed!");
									
								}
							}else
							{
								// process add complaint
								int rs = complaintdao.InsertComplaint(model);
								if(rs==1)
								{
									JOptionPane.showMessageDialog(null, "Insert sucessful!");
									if(login.idrole==0)
										loadData.tab=5;
									else
										loadData.tab=4;
								}
								else
								{
									JOptionPane.showMessageDialog(null, "Insert failed!");
								}
				
							}
							mainForm a = new mainForm();
							a.setVisible(true);
							dispose();
				     }
				     
					//dispose();
					
				}
			});
		}
		return btnOK;
	}

	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnCancel.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnCancel.setText("Exit");
			btnCancel.setLocation(new Point(400, 524));
			btnCancel.setSize(new Dimension(84, 28));
			btnCancel.setHorizontalAlignment(SwingConstants.LEFT);
			btnCancel.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(login.idrole==0)
						loadData.tab=5;
					else
						loadData.tab=4;
					mainForm a = new mainForm();
					a.setVisible(true);
					dispose();
				}
			});
		}
		return btnCancel;
	}
	private void loadDataByID()
	{
		if(panelComplaint.IdCom>=0)
		{
		lbtitle.setText("Update Complaint");
		panelTitle.setSize(new Dimension(150, 28));
		btnOK.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
		btnOK.setText("Save");
		btnOK.setSize(new Dimension(90, 28));
		ArrayList<complaintModel> comp = complaintdao.getdataComplaintByID(panelComplaint.IdCom);
		for(complaintModel model : comp)
		{
			//ComboBoxComplaintype.setText(model.getID_com_type());
			txtname.setText(model.getName());
			txtage.setText(String.valueOf(model.getAge()));
			if(model.getSex()==0)
			{
				Radiofemale.setSelected(true);
			}
			else if(model.getSex()==1)
			{
				radioMale.setSelected(true);
			}
				
			txtAddress.setText(model.getAddress());
			txtPhone.setText(model.getPhone());
			txtEmail.setText(model.getEmail());
			txtEvent.setText(model.getDetail());
			try{
				SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
				txtDate.setDate(sdf.parse(model.getDate()));
			}catch (Exception e) {
				e.printStackTrace();
			}
			txtPlace.setText(model.getPlace());
			txtUser.setText(String.valueOf(model.getIduser()));
		}
	}
	}

	private JTextField getTxtPlace() {
		if (txtPlace == null) {
			txtPlace = new JTextField();
			txtPlace.setLocation(new Point(150, 61));
			txtPlace.setSize(new Dimension(167, 25));
		}
		return txtPlace;
	}

	private JTextField getTxtUser() {
		if (txtUser == null) {
			txtUser = new JTextField();
			txtUser.setLocation(new Point(445, 58));
			txtUser.setEditable(false);
			txtUser.setSize(new Dimension(130, 25));
			txtUser.setText(String.valueOf(mainForm.id));
		}
		return txtUser;
	}
}
